
import VueRouter from 'vue-router';

const routes = [
    {
        path:'',
        redirect:'/Login'
    },
    {
        path:'/Login',
        name:'login',
        component:()=>import('../components/Login'),
        meta:{
            title:'登录'
        }
    },
    {
        path:'/Index',
        name:'index',
        component:()=>import('../components/Index'),
        meta:{
            title:'毕业生SNS',
            requireAuth: true
        },
        children:[
            {
                path:'/Home',
                name:'home',
                meta:{
                    title:'首页',
                    requireAuth: true
                },
                component:()=>import('../components/Home')
            },
            {
                path:'/Announcement',
                name:'announcement',
                meta:{
                    title:'公告',
                    requireAuth: true
                },
                component:()=>import('../components/Announcement')
            },

        ]
    }
]

const router = new VueRouter({
    mode:'history',
    routes
})

export function resetRouter() {
    router.matcher = new VueRouter({
        mode:'history',
        routes: []
    }).matcher
}
const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
    return VueRouterPush.call(this, to).catch(err => err)
}
export  default router;

router.beforeEach((to, from, next) => {
    document.title = to.matched[0].meta.title // 显示标题
    // next()
    if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
        if (sessionStorage.getItem('isLogin') == 1) {
            next()
        } else {
            // 未登录，跳转到登录页面
            next('/Login')
        }
    } else {
        next()
    }
})
